package com.dhwl.hsh.merchant.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.dhwl.hsh.lib.utils.StringUtils;
import com.dhwl.hsh.merchant.R;
import com.dhwl.hsh.merchant.common.Constant;
import com.dhwl.hsh.lib.bean.Address;
import com.dhwl.hsh.merchant.view.DeleteableEditText;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 发货地、配送地 添加、编辑 页面
 *
 * @author Derek.Kidd
 * @time 2016/4/28 14:42
 */
public class AddAddressActivity extends BaseActivity {


    @Bind(R.id.det_deliver_loc)
    DeleteableEditText detLoc;

    @Bind(R.id.det_deliver_name)
    DeleteableEditText detName;

    @Bind(R.id.det_deliver_phone)
    DeleteableEditText detPhone;

    @Bind(R.id.img_array_right)
    ImageView imgArray;

    @Bind(R.id.btn_choose_by_map)
    Button btnMap;

    @Bind(R.id.btn_sure)
    Button btnSure;


    private Address address;

    private String locStr;

    private String nameStr;

    private String phoneStr;

    int billType;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_add_address;
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {

        setTitleContent(R.string.title_shipping_info);

        address = getIntent().getParcelableExtra(Constant.Order.KEY_SHIPPING_ADDRESS);
        billType = getIntent().getIntExtra(Constant.UserInfo.KEY_BILL_TYPE, Constant.Order.VALUE_BILL_TYPE_1);

        //按里程付费，必须地图选址
        if (billType == Constant.Order.VALUE_BILL_TYPE_1) {
            detLoc.setEnabled(false);
            imgArray.setVisibility(View.GONE);
            btnMap.setVisibility(View.VISIBLE);
        } else {
            detLoc.setEnabled(true);
            imgArray.setVisibility(View.VISIBLE);
            btnMap.setVisibility(View.GONE);
        }

        if (address.getIndex() == Constant.Order.VALUE_START_ADDRESS) {
            detLoc.setHint(R.string.hint_input_deliver_loc);
            detName.setHint(R.string.hint_input_deliver_name);
            detPhone.setHint(R.string.hint_input_deliver_phone);
        } else {
            detLoc.setHint(R.string.hint_input_receive_loc);
            detName.setHint(R.string.hint_input_receive_name);
            detPhone.setHint(R.string.hint_input_receive_phone);
        }


        if (!TextUtils.isEmpty(address.getPosiName())) {
            detLoc.setText(address.getPosiName());
        }
        if (!TextUtils.isEmpty(address.getLinkManName())) {
            detName.setText(address.getLinkManName());
        }
        if (!TextUtils.isEmpty(address.getLinkManMobile())) {
            detPhone.setText(address.getLinkManMobile());
        }

    }

    @Override
    protected void initData() {

    }

    @OnClick(R.id.btn_choose_by_map)
    public void onMapChooseClick(View view) {
        Intent intent = new Intent(this, MapChooseActivity.class);
        startActivityForResult(intent, Constant.Order.REQUEST_CODE_22);
    }

    @OnClick(R.id.btn_sure)
    public void onSureClick(View view) {
        locStr = detLoc.getText();
        nameStr = detName.getText();
        phoneStr = detPhone.getText();
        if (TextUtils.isEmpty(locStr)) {
            showToast(R.string.hint_input_deliver_loc);
            return;
        }
        if (TextUtils.isEmpty(nameStr)) {
            showToast(R.string.hint_input_deliver_name);
            return;
        }
        if (TextUtils.isEmpty(phoneStr)) {
            showToast(R.string.hint_input_deliver_phone);
            return;
        }
        if (!StringUtils.isPhone(phoneStr)) {
            showToast(R.string.input_phone_is_error);
        }

        address.setPosiName(locStr);
        address.setLinkManName(nameStr);
        address.setLinkManMobile(phoneStr);

        Intent intent = new Intent();
        intent.putExtra(Constant.Order.KEY_SHIPPING_ADDRESS, (Parcelable) address);
        setResult(RESULT_OK, intent);
        finish();

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK && Constant.Order.REQUEST_CODE_22 == requestCode) {
            Address add = data.getParcelableExtra(Constant.Order.KEY_ADDRESS);
            address.setPosiName(add.getPosiName());
            address.setPosiSubName(add.getPosiSubName());
            address.setPosiLatit(add.getPosiLatit());
            address.setPosiLongi(add.getPosiLongi());

            detLoc.setText(address.getPosiName());
        }
    }
}
